var should = require("should");
var request = require("superagent");
var db = require("../models/index");
var utilx = require("../lib/util");
var host = "http://127.0.0.1:3000";
var cache = require("../instances/cache");
var User = db.models.User;
var Story = db.models.Story;
var Comment = db.models.Comment;


describe("test debugLogin and post data", function () {
    var LoginToken;
    var token;
    var userID;
    var storyID;
    var commentID;

    before(function (done) {
        request
            .get(host + "/api/debugLogin")
            .end(function (err, res) {
                token = res.text;
                request
                    .post(host + "/api/getToken")
                    .send({token: token})
                    .end(function (err, res) {
                        var data = JSON.parse(res.text);
                        LoginToken = data.msg.LoginToken;
                        userID = data.msg.userID;
                        done();
                    });
            });
    });

    it("post public storylist with timestamps should get all public stories",function(done){
        var timestamps = Math.round(new Date().getTime()/1000);
         request
             .post(host + "/api/token/public/storylist/1/20")
             .set("LoginToken",LoginToken)
             .send({timestamps:timestamps})
             .end(function(err,res){
                 try {
                     var data = JSON.parse(res.text);
                 } catch (e) {
                     console.log(e);
                 }
                 data.status.should.be.exactly(true);
                 data.msg.length.should.be.exactly(20);
                 done();
             });
    });

    it("post own storylist should get story data", function (done) {
        // console.log("userID" + userID);
        request
            .post(host + "/api/token/storylist/1/8")
            .set("LoginToken", LoginToken)
            .send({userID: userID})
            .end(function (err, res) {
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(e);
                }
                //console.log(res.text);
                data.status.should.be.exactly(true);
                data.msg.user.id.should.be.exactly(userID);
                data.msg.stories.length.should.be.exactly(8);
                data.msg.stories[0].likeCount.should.be.exactly(10);
                // console.log(data.msg.stories[0].likeCount);
                done();
            });
    });

    it("post storyID which own by me should get story", function (done) {
        Story.findOne({
            where: {
                AuthorID: userID
            }
        }).then(function (story) {
            request
                .post(host + "/api/token/story")
                .set("LoginToken", LoginToken)
                .send({storyID: story.id, AuthorID: userID})
                .end(function (err, res) {
                    try {
                        var data = JSON.parse(res.text);
                        console.log(data);
                    } catch (e) {
                        console.log(e);
                    }
                    data.status.should.be.exactly(true);
                    data.msg.isOwn.should.be.exactly(true);
                    data.msg.story.likeCount.should.be.exactly(10);
                    data.msg.story.content.should.be.exactly("hello world");
                    done();
                });
        });
    });

    it("post storyID which not own by me should get story", function (done) {
        Story.findOne({
            where: {
                AuthorID: {
                    $ne: userID
                }
            }
        }).then(function (story) {
            request
                .post(host + "/api/token/story")
                .set("LoginToken", LoginToken)
                .send({storyID: story.id, AuthorID: story.AuthorID})
                .end(function (err, res) {
                    try {
                        var data = JSON.parse(res.text);
                    } catch (e) {
                        console.log(e);
                    }
                    // console.log(res.text);
                    data.status.should.be.exactly(true);
                    data.msg.isOwn.should.be.exactly(false);
                    data.msg.story.likeCount.should.be.exactly(10);
                    data.msg.story.content.should.be.exactly("hello world");
                    done();
                });
        });
    });
    

    it("collect a story msg should return true", function (done) {
        Story.findOne({}).then(function (story) {
            request
                .post(host + "/api/token/story/collect")
                .set("LoginToken", LoginToken)
                .send({storyID: story.id, isCollect: 1})
                .end(function (err, res) {
                    try {
                        var data = JSON.parse(res.text);
                    } catch (e) {
                        console.log(e);
                    }
                    data.status.should.be.exactly(true);
                    data.msg.should.be.exactly(true);
                    done();
                });
        });
    });

    it("like a story msg should return true", function (done) {
        Story.findOne({}).then(function (story) {
            request
                .post(host + "/api/token/story/like")
                .set("LoginToken", LoginToken)
                .send({storyID: story.id, isLike: 1})
                .end(function (err, res) {
                    try {
                        var data = JSON.parse(res.text);
                    } catch (e) {
                        console.log(e);
                    }
                    data.status.should.be.exactly(true);
                    data.msg.should.be.exactly(true);
                    done();
                });
        });
    });

    it("should return commentlist", function (done) {
        Story.findOne({}).then(function (story) {
            storyID = story.id;
            request
                .post(host + "/api/token/commentlist/" + storyID)
                .set("LoginToken", LoginToken)
                .end(function (err, res) {
                    try {
                        var data = JSON.parse(res.text);
                    } catch (e) {
                        console.log(e);
                    }
                    data.status.should.be.exactly(true);
                    data.msg.comment[0].content.should.be.exactly("this is a test comment of article");
                    data.msg.comment[0].RevComment[0].content.should.be.exactly("this is a test comment of comment");
                    done();
                });
        });
    });

    it("post a comment should work ok", function (done) {
        request
            .post(host + "/api/token/comment/" + storyID)
            .set("LoginToken", LoginToken)
            .send({commentContent: "this is a comment of article from test"})
            .end(function (err, res) {
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(e);
                }
                data.status.should.be.exactly(true);
                commentID = data.msg.id;
                Comment.findOne({
                    where: {
                        id: data.msg.id
                    }
                }).then(function (result) {
                    result.should.be.ok;
                    done();
                });
            });
    });

    it("post a reply should work ok", function (done) {
        request
            .post(host + "/api/token/reply/" + commentID)
            .set("LoginToken", LoginToken)
            .send({replyContent: "this is a comment of comment from test"})
            .end(function (err, res) {
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(e);
                }
                data.status.should.be.exactly(true);
                Comment.findOne({
                    where: {
                        id: data.msg.id
                    }
                }).then(function (result) {
                    result.should.be.ok;
                    done();
                });
            });
    });

    it("post a story should work ok",function(done){
        request
            .post(host + "/api/token/addstory")
            .set("LoginToken", LoginToken)
            .send({title : "this is a test",flags : 'what',content : "this is a test",publicEnable : 1})
            .end(function (err, res) {
                console.log(res.status);
                try {
                    var data = JSON.parse(res.text);
                    console.log(data);
                } catch (e) {
                    console.log(e);
                }
                data.status.should.be.exactly(true);
                done();
            });
    });
});